package com.tzt.controller.home;

import com.tzt.common.BaseController;
import com.tzt.contents.ErrorCodeConstant;
import com.tzt.entity.InventoryWarningRulesEntity;
import com.tzt.entity.OrderEntity;
import com.tzt.entity.StorageLogEntity;
import com.tzt.entity.UserEntity;
import com.tzt.exception.InnerException;
import com.tzt.service.HomeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author harryleng
 */
@RestController
public class HomeController extends BaseController
{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private HomeService homeService;

    /**
     * 查询当天销售
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param loginRoleId 登录角色id
     * @return
     */
    @RequestMapping(value = "/api/getTodayCount", method = RequestMethod.POST)
    public Map<String, Object> getToDaySales(String shopUserId, String token,String
            loginRoleId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            BigDecimal geToDaySales = homeService.geToDaySales(shopUserId,
                    token,loginRoleId);
            map.put("todayCount", geToDaySales);
        }
        catch (NumberFormatException e)
        {
            logger.error("查询当天销售：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("查询当天销售：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("查询当天销售：", e);
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }

    /**
     * 查询总销售
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param loginRoleId 登录角色id
     * @return
     */
    @RequestMapping(value = "/api/getTotalSales", method = RequestMethod.POST)
    public Map<String, Object> getTotalSales(String shopUserId, String token,String
            loginRoleId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            BigDecimal totalSales = homeService.getTotalSales
                    (shopUserId, token,loginRoleId);
            map.put("totalSales", totalSales);
        }
        catch (NumberFormatException e)
        {
            logger.error("查询总销售：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("查询总销售：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("查询总销售：", e);
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }

    /**
     * 查询首页top商品
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param qryStatus
     * @param loginRoleId 登录角色id
     * @return
     */
    @RequestMapping(value = "/api/getHomeTopProduct", method = RequestMethod.POST)
    public Map<String, Object> getHomeTopProduct(String shopUserId, String token, String qryStatus,String loginRoleId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            map = homeService.getHomeTopProduct(shopUserId, token, qryStatus,loginRoleId);
        }
        catch (NumberFormatException e)
        {
            logger.error("查询首页top商品：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("查询首页top商品：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("查询首页top商品：", e);
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }

    /**
     * 查询统计金额
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param loginRoleId 角色登录id
     * @return
     */
    @RequestMapping(value = "/api/getStatisAmount", method = RequestMethod.POST)
    public Map<String, Object> getStatisAmount(String shopUserId, String token, String startTime, String endTime,String loginRoleId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            List<OrderEntity> statisAmount = homeService.getStatisAmount
                    (shopUserId, token, startTime, endTime,loginRoleId);
            map.put("statisAmount", statisAmount);
        }
        catch (NumberFormatException e)
        {
            logger.error("查询统计金额：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("查询统计金额：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("查询统计金额：", e);
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }

    /**
     * 查询首页设备数和设备故障数
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param loginRoleId 登录角色id
     * @return
     */
    @RequestMapping(value = "/api/getHomeTerminalCount", method = RequestMethod.POST)
    public Map<String, Object> getHomeTerminalCount(String shopUserId, String token,String loginRoleId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            map = homeService.getHomeTerminalCount(shopUserId, token,loginRoleId);

        }
        catch (NumberFormatException e)
        {
            logger.error("查询首页设备数和设备故障数：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("查询首页设备数和设备故障数：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("查询首页设备数和设备故障数：", e);
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }

    /**
     * 获取新增会员
     * @param shopUserId 商家用户id
     * @param token 验签
     * @return
     */
    @RequestMapping(value = "/api/getNewMembers", method = RequestMethod.POST)
    public Map<String, Object> getNewMembers(String shopUserId, String token)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            List<UserEntity> newMembers = homeService.getNewMembers(shopUserId, token);
            map.put("newMembers", newMembers);
        }
        catch (NumberFormatException e)
        {
            logger.error("获取新增会员：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("获取新增会员：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("获取新增会员：", e);
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }

    /**
     * 查询配送员
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param loginRoleId 登录角色id
     * @return
     */
    @RequestMapping(value = "/api/getMarkis", method = RequestMethod.POST)
    public Map<String, Object> getMarkis(String shopUserId, String token,String loginRoleId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            List<StorageLogEntity> distribution = homeService.getDistribution
                    (shopUserId, token,loginRoleId);
            map.put("distribution", distribution);
        }
        catch (NumberFormatException e)
        {
            logger.error("查询商品配送：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("查询商品配送：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("查询商品配送：", e);
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }

    /**
     * <功能详细描述>查询有预警商品的规则
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param loginRoleId 登录角色id
     * @return
     */
    @RequestMapping(value = "/api/getInventoryWarningRules", method = RequestMethod.POST)
    public Map<String, Object> getInventoryWarningRules(String shopUserId, String token,String loginRoleId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            List<InventoryWarningRulesEntity> inventoryWarningRules = homeService.getInventoryWarningRules(shopUserId, token,
                            loginRoleId);
            map.put("inventoryWarningRules", inventoryWarningRules);
        }
        catch (NumberFormatException e)
        {
            logger.error("查询有预警商品的规则：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("查询有预警商品的规则：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("查询有预警商品的规则：", e);
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }
}
